//
//  UIView+Frame.m
//  XiaoMiao
//
//  Created by 23 on 2017/11/4.
//  Copyright © 2017年 smallyou. All rights reserved.
//

#import "UIView+Frame.h"

@implementation UIView (Frame)

- (void)setApp_width:(CGFloat)app_width {
    CGRect frame = self.frame;
    frame.size.width = app_width;
    self.frame = frame;
}

- (CGFloat)app_width {
    return self.frame.size.width;
}

- (void)setApp_height:(CGFloat)app_height {
    CGRect frame = self.frame;
    frame.size.height = app_height;
    self.frame = frame;
}

- (CGFloat)app_height {
    return self.frame.size.height;
}

- (void)setApp_centerX:(CGFloat)app_centerX {
    CGPoint center = self.center;
    center.x = app_centerX;
    self.center = center;
}

- (CGFloat)app_centerX {
    return self.center.x;
}

- (void)setApp_centerY:(CGFloat)app_centerY {
    CGPoint center = self.center;
    center.y = app_centerY;
    self.center = center;
}
- (CGFloat)app_centerY {
    return self.center.y;
}

- (void)setApp_x:(CGFloat)app_x {
    CGRect frame = self.frame;
    frame.origin.x = app_x;
    self.frame = frame;
    
}
- (CGFloat)app_x {
    return self.frame.origin.x;
}

- (void)setApp_y:(CGFloat)app_y {
    CGRect frame = self.frame;
    frame.origin.y = app_y;
    self.frame = frame;
    
}

- (CGFloat)app_y {
    return self.frame.origin.y;
}

- (void)setApp_left:(CGFloat)app_left {
    self.app_x = app_left;
}

- (CGFloat)app_left {
    return self.app_x;
}

- (void)setApp_top:(CGFloat)app_top {
    self.app_y = app_top;
}

- (CGFloat)app_top {
    return self.app_y;
}

- (void)setApp_right:(CGFloat)app_right {
    CGRect frame = self.frame;
    frame.origin.x = app_right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)app_right {
    return CGRectGetMaxX(self.frame);
}

- (void)setApp_bottom:(CGFloat)app_bottom {
    CGRect frame = self.frame;
    frame.origin.y = app_bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)app_bottom {
    return CGRectGetMaxY(self.frame);
}

- (void)app_removeAllSubviews {
    while (self.subviews.count) {
        UIView* child = self.subviews.lastObject;
        [child removeFromSuperview];
    }
}

- (id)app_subviewWithTag:(NSInteger)tag {
    
    for(UIView *view in [self subviews]){
        if(view.tag == tag){
            return view;
        }
    }
    return nil;
}

- (BOOL)app_containterSubView:(UIView *)subView {
    for (int i = 0; i < self.subviews.count; i++) {
        if ([self.subviews[i] isEqual:subView]) {
            return YES;
        }
    }
    
    return NO;
}

+ (instancetype)app_viewFromXib {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

@end
